﻿using Gx.Common;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Model;
using Gx.Model.Entity;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Gx.Bll.Manage
{
    public class LKCustomerLevelBll
    {
        public List<LKCustomerLevel> GetLKCustomerLevels()
        {
            BaseDal<LKCustomerLevel> dal = new BaseDal<LKCustomerLevel>();
            return dal.GetAll().ToList();
        }

        /// <summary>
        /// 新增客户等级
        /// </summary>
        /// <param name="customerLevel"></param>
        /// <returns></returns>
        public ReturnModel AddCustomerLevel(LKCustomerLevel customerLevel)
        {
            LKCustomerLevelDal dal = new LKCustomerLevelDal();

            var any = dal.AnyCustomerLevelName(customerLevel.LevelName);
            if (any)
            {
                return new ReturnModel { Success = false, Msg = "客户等级名称已存在" };
            }


            customerLevel.CreateTime = DateTime.Now;
            customerLevel.CreateUserId = LoginHelper.GetUserId();
            customerLevel.CreateUser = LoginHelper.UserName();
            var result= dal.Insert(customerLevel);
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑客户等级
        /// </summary>
        /// <param name="customerLevel"></param>
        /// <returns></returns>
        public bool EditCustomerLevel(LKCustomerLevel customerLevel)
        {
            BaseDal<LKCustomerLevel> dal = new BaseDal<LKCustomerLevel>();
            var cus = dal.Get(customerLevel.Id);
            if (cus == null)
            {
                return false;
            }
            customerLevel.CreateTime = cus.CreateTime;
            customerLevel.CreateUserId = cus.CreateUserId;
            customerLevel.CreateUser = cus.CreateUser;
            return dal.Update(customerLevel);
        }

        /// <summary>
        /// 删除客户等级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteCustomerLevel(int id)
        {
            BaseDal<LKCustomerLevel> dal = new BaseDal<LKCustomerLevel>();
            return dal.DeleteById(id);
        }
    }
}
